<template>
  <div class="role">
    <page-search :searchFormConfig="searchFormConfig"></page-search>
    <page-content
      :contentTableConfig="contentTableConfig"
      pageName="role"
      @newBtnClick="handleNewData"
      @editBtnClick="handleEditData"
    ></page-content>
    <page-modal
      :modalConfig="modalConfig"
      :defaultInfo="defaultInfo"
      :otherInfo="otherInfo"
      ref="pageModalRef"
      pageName="user"
    >
      <div class="menu-tree">
        <el-tree
          ref="elTreeRef"
          :data="menus"
          show-checkbox
          node-key="id"
          :props="{ children: 'children', label: 'name' }"
          @check="handleCheckChange"
        >
        </el-tree>
      </div>
    </page-modal>
  </div>
</template>

<script lang="ts" setup>
import PageContent from '@/components/page-content';
import PageSearch from '@/components/page-search';
import { usePageModalHooks } from '@/hooks/use-page-modal';
import store from '@/store';
import { menuMapLeafKeys } from '@/utils/map-menus';
import { ElTree } from 'element-plus';
import { computed, nextTick, ref } from 'vue';

import { contentTableConfig } from './config/content.config';
import { modalConfig } from './config/modal.config';
import { searchFormConfig } from './config/search.config';

const editCallback = (item: any) => {
  const leafKeys = menuMapLeafKeys(item.menuList);
  nextTick(() => {
    elTreeRef.value?.setCheckedKeys(leafKeys, false);
  });
};
const [pageModalRef, defaultInfo, handleNewData, handleEditData] = usePageModalHooks(
  undefined,
  editCallback
);
const menus = computed(() => store.state.entireMenu);
const otherInfo = ref({});
const handleCheckChange = (data1: any, data2: any) => {
  const checkedKeys = data2.checkedKeys;
  const halfCheckedKeys = data2.halfCheckedKeys;
  const menuList = [...checkedKeys, ...halfCheckedKeys];
  otherInfo.value = { menuList };
};

const elTreeRef = ref<InstanceType<typeof ElTree>>();
</script>

<style scoped lang="less">
.menu-tree {
  margin-left: 25px;
}
</style>
